package com.users.rn.rncommon.plugin;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;

/**
 * desc:解决拆包路径问题
 * 作者：wolf on 12/25/20 10:19
 * <p>
 * 邮箱：bluce
 */

public class ReactSourceCode extends ReactContextBaseJavaModule {

    public ReactSourceCode(ReactApplicationContext context) {
        super(context);
    }

    @NonNull
    @Override
    public String getName() {
        return ReactSourceCode.class.getSimpleName();
    }

    /**
     * 不建议使用 google chrome不支持debug
     * @return
     */
    @Deprecated
    @ReactMethod(isBlockingSynchronousMethod = true)
    public String getSourceCode() {
        String sourceUrl = "";
        if (getReactApplicationContext() != null) {
            sourceUrl = getReactApplicationContext().getSourceURL();
        }
        return sourceUrl;
    }

    @ReactMethod
    public void getSourceUrl(Promise promise) {
        WritableMap result = Arguments.createMap();
        if (getReactApplicationContext() != null && !TextUtils.isEmpty(getReactApplicationContext().getSourceURL())) {
            result.putString("sourceURL", getReactApplicationContext().getSourceURL());
        }
        promise.resolve(result);
    }

}